// ============ class for stop watch show how long time studying for ============ //
class cstopWatch {
  // === constructor === //
  cstopWatch(int x, int y, color fntColor, int tmo, String thread) {
    this.x = x;
    this.y = y;
    this.tmo = tmo;

    layer = createGraphics(100, 40);
    layer.beginDraw();
    layer.fill(fntColor);
    layer.textFont(mainFont);
    layer.textSize(40);
    layer.endDraw();

    thread(thread); // 1 sec thread for stopwatch
  }

  // ======= //
  void show() {
    image(layer, x, y);
  }

  // ====== //
  void count() { // call it every 1 second
    if (++sec >= 60) {
      sec = 0;
      if (++min >= tmo) {
        println("good job! reciting time out. take a rest!");
        exit();
      }
    }
    timeStr = nf(min, 2, 0) + ":" + nf(sec, 2, 0);
    layer.beginDraw();
    layer.clear();
    layer.text(timeStr, 0, 30);
    layer.endDraw();
  }

  // ======= //
  private int x, y;
  private int sec = 0;
  private int min = 0;
  private PGraphics layer;
  private String timeStr;
  private int tmo; // unit = minute
};

// ====== concrete object ====== //
cstopWatch stopWatch;

// ============ thread for counting up stopwatch every 1 second ============ //
void thread1sec() {
  while (true) {
    stopWatch.count();
    delay(1000);
  }
}
